<?php	include_once('config/db.php');
	include_once('functions/item.php');
	include_once('functions/category.php');
if(!isset($_SESSION['logged_in'])) 
header('location:login.php');

$items = getAvailableItems();
$category = getCategories();
if(isset($_REQUEST['submit'])){	
	$cat_items = getCategoryItems($_REQUEST['cat_item']);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="style.css" type="text/css"/>
<script type="text/javascript" src="assets/script/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="assets/script/script.js"></script>
<title>Online Reservation System</title>
</head>

<body>
<div id="header">
<div id="logo">
  <p>&nbsp;</p>
  <div id="apDiv1"><img src="image/logo.png" alt="" width="500" height="100" /></div>
  <p>&nbsp;</p>
</div>
</div>
<div id="con-body">
 <h4>Welcome: <a href="index.php"><?php echo $_SESSION['name'];?></a> | <a href="change.php">Change Password</a> | <a href="edit.php">Edit Profile</a> | <a href="logout.php">Logout</a>  </h4>
<?php include('extension/left_bar.php');?>

 <div id="right_container">
   <h4>List of Reserved Materials: </h4>
    <form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
     <label for="textfield"></label>
     <input type="text" name="tracking" id="tracking" />
     <input type="submit" name="cancel_button" id="cancel_button" value="Submit" />
   </form>
       <?php 
		if(isset($_REQUEST['tracking'])){
			$track=$_POST['tracking'];
			$user_id=$_SESSION['user_id'];
			$query=mysql_query("SELECT * FROM user LEFT JOIN borrow ON user.user_id = borrow.user_id LEFT JOIN item ON borrow.item_id = item.item_id
			WHERE tracking = '$track' AND user.user_id= '$user_id' GROUP BY tracking");
	    ?>
    <?php while($row=mysql_fetch_array($query)){?>
    <form id="form2" name="form2" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
        <p>Tracking Number: <?php echo $row['tracking'];?></p>
        <input type="hidden" name="item_id" id="item_id" value="<?php echo $row['item_id']; ?>"/>
    <p>ID number: <?php echo $row['user']; ?></p>
    <p>Name: <?php echo $row['name'];?></p>
    <p>Contact: <?php echo $row['contact'];?></p>
    <p>Email <?php echo $row['email'];?>:</p>
    <!--<p>Barrow date: From- <?php //echo $row['from_date']; ?>To- <?php //echo $row['to_date']; ?></p>
    <p>Item Status: <?php //echo $row['item_status']; ?></p>-->
<table width="990" border="0">
  <tr>
    <td width="110" bgcolor="#99FFCC">Item Name</td>
    <td width="141" bgcolor="#99FFCC">Serial</td>
    <td width="198" bgcolor="#99FFCC">Requiring Subject</td>
    <td width="241" bgcolor="#99FFCC">Item Condition</td>
    <td width="91" bgcolor="#99FFCC">Date From</td>
    <td width="88" bgcolor="#99FFCC">Date To</td>
    <td width="91" bgcolor="#99FFCC">Date to return</td>
  </tr>
</table>
<?php 
if(isset($_REQUEST['tracking'])){
	$track=$_REQUEST['tracking'];
	$query1=mysql_query("SELECT * FROM borrow LEFT JOIN item ON item.item_id = borrow.item_id WHERE tracking = '$track'");
?>
<?php while($row1=mysql_fetch_array($query1)){?>
<table width="990" border="0">
  <tr>
    <td width="110" bgcolor="#FFFF99"><?php echo $row1['item_name']?></td>
    <td width="143" bgcolor="#FFFF99"><?php echo $row1['item_serial']?></td>
    <td width="198" bgcolor="#FFFF99"><?php echo $row1['requiring_subject']?></td>
    <td width="243" bgcolor="#FFFF99"><?php echo $row1['item_condition']?></td>
    <td width="90" bgcolor="#FFFF99"><?php echo $row1['from_date']?></td>
    <td width="87" bgcolor="#FFFF99"><?php echo $row1['to_date']?></td>
    <td width="89" bgcolor="#FFFF99"><?php echo $row1['pickup_time']?></td>
  </tr>
</table>
  <?php }}?>
  </br>
</p>
<form id="form" name="form" method="post" action="">
      <p>
        <label for="select"></label>
        <select name="item_status" id="item_status" value="">
          <option value="">------------------</option>
          <option value="">CANCEL</option>
        </select>
      </p>
      <p>
        <input name="update_button" type="submit" id="update_button" value="Submit" />
      </p>
</form>
    <?php }}?>
    	<?php 
if(isset($_REQUEST['update_button'])){
		$item_id=$_REQUEST['item_id'];
		//$item_status=$_REQUEST['item_status'];
		$update_kweri=mysql_query("UPDATE item SET item_status='AVAILABLE'") or die('ERROR:' .mysql_error());
		echo'RETURNED';
}
?>
 </div>
 
 <div class="container">
 	<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
   		Choose Category: <select name="cat_item">
			<?php if($category != 0 ):?>
				<?php foreach($category as $c):?>
					<?php if(isset($_REQUEST['submit'])): ?>
					<option value="<?php echo $c['category_id']?>" <?php echo $_REQUEST['cat_item'] == $c['category_id'] ?'selected="selected"': ''?>><?php echo $c['category_name']?></option>
					<?php else: ?>
					<option value="<?php echo $c['category_id']?>" ><?php echo $c['category_name']?></option>
					<?php endif;?>
			
				<?php endforeach;?>
			<?php endif;?>
					<option value="all"<?php if(isset($_REQUEST['submit'])) echo $_REQUEST['cat_item'] == 'all' ? 'selected="selected"':''; ?>>All categories</option>
		</select>
		<input type="submit" value="Submit" name="submit" />
   </form>
 </div>
 
 <?php if(isset($cat_items)):?>
 <div class="container">
 		<?php if($cat_items != 0):?>
			<form method="post" action="borrowers.php">
			<table width="100%" border="0">
			<thead>
				<tr>
					<th>ID</th>
					<th>Name</th>
					<th>Description</th>
					<th></th>
				</tr>
			</thead>
			<tbody>
			<?php foreach($cat_items as $i):?>
				<?php $ctr = 1;?>
				<tr>
					<td><input type="checkbox" name="i[]" value="<?php echo $i['item_id']?>" style="display:none" /><?php echo $i['item_id']?></td>
					<td><?php echo $i['item_name']?></td>
					<td><?php echo $i['item_desc']?></td>
					<td><a href="#" class="reserve">Click to reserve</a></td>
				</tr>
				<?php $ctr++;?>
			<?php endforeach;?>
			</tbody>
			</table>
			<input type="submit" name="save_item" value="Next" />
			</form>
		<?php else:?>
			<div class="notice">No items available for this category</div>
		<?php endif;?>
 </div>
 <?php endif;?>
<div id="clear">
  <p><?php 
		$viewerIp = md5($_SERVER["REMOTE_ADDR"]);
		$viewers=mysql_query("SELECT * FROM viewers WHERE viewersIp='$viewerIp'");
		$result=mysql_num_rows($viewers);
			if($result==0){
				$Users_IP_address = md5($_SERVER["REMOTE_ADDR"]);
				$viewer=mysql_query("INSERT INTO viewers (viewersId,viewersIp)VALUES('$viewerIp','$Users_IP_address')");

				}else{
					$myviewers=mysql_query("SELECT * FROM viewers");
					$myresult=mysql_num_rows($myviewers);
						echo '';
}
?>&nbsp;</p>
</div>
</div>
</body>
</html>